package com.web.surf.sample.eshop.utils;

import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.faces.context.FacesContext;

public class MessageProvider {

	private static ResourceBundle bundle;

	public static ResourceBundle getBundle(String bundleName) {
		FacesContext context = FacesContext.getCurrentInstance();
		bundle = context.getApplication()
				.getResourceBundle(context, bundleName);
		return bundle;
	}

	public static String getValue(String key, String bundleName) {
		String result = null;
		try {
			result = getBundle(bundleName).getString(key);
		} catch (MissingResourceException e) {
			result = "???" + key + "??? not found";
		}
		return result;
	}

	public static SortedSet<String> getValues(String bundleName) {
		SortedSet<String> values = new TreeSet<String>();
		try {
			Set<String> keys = getBundle(bundleName).keySet();			
			for (String value : keys) {
				values.add(getBundle(bundleName).getString(value));
			}
		} catch (MissingResourceException e) {
			values = null;
		}
		return values;
	}
}
